<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-row type="flex" justify="space-between">
          <el-col>
            <el-button type="primary" @click="addFn">添加分类</el-button>
          </el-col>
          <el-col>
            <el-input v-model.trim="value" placeholder="请输入关键字">
              <el-button slot="append" icon="el-icon-search" @click="serch" /></el-input>
          </el-col>
        </el-row>

      </el-card>
      <el-card>
        <el-table
          :data="tableData"
          border
          style="width: 100%"
        >
          <el-table-column prop="image" label="图片" width="200px">
            <template v-slot="{row}">
              <img :src="row.image" class="imgtable">
            </template>
          </el-table-column>
          <el-table-column prop="title" label="标题" />
          <el-table-column prop="desc" label="介绍" />

          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button type="primary">编辑</el-button>
              <el-button type="danger" @click="delFn(row.id)">删除</el-button>

            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="center">

          <el-pagination
            layout="prev, pager, next"
            :total="total"
            background
            :page-size="limit"
            @current-change="curr"
          />
        </el-row>
      </el-card>
      <addCate :show-dialog.sync="showDialog" />
    </div>
  </div>
</template>

<script>
import { categoryList, delcategory, serchcategory, categoryNum } from '@/api/category'
import addCate from './components/addcate.vue'
export default {
  components: { addCate },
  inject: ['reload'],

  data() {
    return {
      tableData: [
      ],
      showDialog: false,
      value: '',
      total: 0, // 总数
      limit: 5, // 每页显示的条数
      page: 1, // 页码
      start: 0 // 页码第一页
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 查询
    async serch() {
      const res = await serchcategory(this.value)
      console.log('查询', res)
      this.tableData = res.data
    },
    // 删除
    delFn(id) {
      // console.log(id)
      this.$confirm('是否确定删除', '提示', {
        type: 'warning'
      }).then(async() => {
        // console.log('点击了确定按钮')
        await delcategory(id)
        // 刷新列表
        this.reload()
      }).catch((err) => {
        console.log('点击了取消按钮', err)
      })
      this.reload()
    },
    // 添加
    addFn() {
      this.showDialog = true
    },
    getList() {
      this.categoryNum()
      this.GetcategoryList()
    },
    // 翻页
    curr(cur) {
      this.page = cur
      this.start = (this.page - 1) * this.limit
      // 加载列表
      this.getList()
    },
    // 获取列表总数量
    async categoryNum() {
      const res = await categoryNum(

      )
      console.log('总数量为', res.data)
      this.total = res.data
    },

    // 获取列表数据
    async GetcategoryList() {
      const res = await categoryList(
        {
          _limit: this.limit,
          _start: this.start
        }
      )
      console.log('64', res)
      // 渲染到表格
      this.tableData = res.data
      // this.lenght = res.lenght
    }
  }
}
</script>

  <style scoped>
  .imgtable{
    width: 200px;
    height: 200px;

  }

  </style>

